
$(function () {
    init();
})

function init() {
    FastClick.attach(document.body);
    $("#elist").on("click", function () {
        window.location.href = "/elist"
    })
    bindEvent();
    loadCookie();
}

function loadCookie() {
    $("#history").html("<div class=\"button noInfo\"><span>无查询记录</span></div>");
    equipmemtService.queryEquipmentHis(function (history) {
        if(history!=null) $("#history").html("");
        for(var code in history){
            var name=history[code].name.split("-");
            var fullname=history[code].name.substring(name[0].length+name[1].length+2);
            var button=$("<div class=\"button\" style='background-image: url("+history[code].externalPhotos+")' onclick='window.location.href = \"/edetail/"+code+"\"' value='"+code+"'><span class=\"type\">"+name[0]+"</span><br><span>"+fullname+"</span></div>");
            $("#history").append(button);
        }
        $(".button").height(($(".button").width()-10)+"px");

    })
}
function selectUserEquipmentList() {
    equipmemtService.queryEquipmentList({state:"203001,203002",searchKey:$("#searchInput").val()}, function (data) {
        if (data.success) {
            var serverData = data.datas;
            loadUInformation2(serverData,$("#searchBox"));
        } else {
            $.alert(data.errorMsg);
        }
    })
}
function loadUInformation2(serverData,group) {
    group.html("");
    $(serverData).each(function (index, item) {
        var name=item.name.split('-');
        var row = $(' <div class="row">\n' +
            '                        <div class="title">'+name[1]+name[2]+'</div>\n' +
            '                        <div class="subTitle">'+item.orgName+'-'+item.usePosition+'</div>\n' +
            '                        <div class="state"><span class="fa fa-tools"><br>'+baseService.getServerConstNameByCode("state",item.state)+'</span></div>\n' +
            '                        <div class="secret"><span class="fa fa-lock"><br>'+baseService.getServerConstNameByCode("secrecy",item.secrecy)+'</span></div>\n' +
            '                        <div class="flex">\n' +
            '                            <div class="label">领用日期</div>\n' +
            '                            <div class="value">2019年12月18日</div>\n' +
            '                        </div>\n' +
            '                        <div class="flex">\n' +
            '                            <div class="label">累计维修</div>\n' +
            '                            <div class="value">'+(item.mcount==""?0:item.mcount)+'次</div>\n' +
            '                        </div>\n' +
            '                        <div class="flex">\n' +
            '                            <div class="label">设备编码</div>\n' +
            '                            <div class="value">'+item.barCode+'</div>\n' +
            '                        </div>\n' +
            '                        <div class="flex">\n' +
            '                            <div class="label">二维码</div>\n' +
            '                            <div class="value">'+item.QRcodeName+'</div>\n' +
            '                        </div>\n' +
            '                    </div>');
        group.append(row);
        row.on("click", function () {
            window.location.href = "/edetail/"+item.QRcodeName;
        })
    })
}

function bindEvent(){
    $('body').on('submit', '#searchBar', function(event){
        //todo ...
        selectUserEquipmentList()
    })

    $("#delete").on("click", function () {
        $.confirm("是否要删除查询记录","确认删除",function () {
            delCookie("eHistory");
            loadCookie();
        })
    })
}
